var ws, spage, pid, uid, doctorname, davatar; 
var aid, name, time, num, eatnotice, runnotice, histor,ztime, wtype;
document.addEventListener('plusready', function(){
	ws = plus.webview.currentWebview();
	aid = ws.aid;
	spage = ws.spage;
	pid = ws.pid;
	name = ws.name;
	histor = ws.histor;
	ztime = ws.ztime;
	newly = ws.newly;
	time = ws.time; 
	num = ws.num; 
	eatnotice = ws.eatnotice;
	runnotice = ws.runnotice;
	doctorname = ws.doctorname;
	davatar = ws.davatar;
	wtype = ws.wtype;
console.log(aid+" "+name+" "+time+" "+num+" "+eatnotice+" "+runnotice+" "+histor+" "+ztime+" "+wtype);
	uid = plus.storage.getItem('uid');
	doctorname = plus.storage.getItem('name')?plus.storage.getItem('name'):plus.storage.getItem('user');
	davatar = plus.storage.getItem('userAvatar') != 'null'?plus.storage.getItem('userAvatar'):'';//医生头像
	
	//点击上传图片
	$('.uploadPic').on('tap', function(){
		mui('#sheet1').popover('toggle');
	})
	//选择拍照添加
	$('.paizhao').on('tap', function(){
		getImage();
	})
	//选择相册添加
	$('.photo').on('tap', function(){
		appendByGalleryMul();
	})
	function getImage() {
		var cmr = plus.camera.getCamera();
		cmr.captureImage(function(path) {
			var path = plus.io.convertLocalFileSystemURL(path);
			$('.zz-img').append('<img src="'+path+'" data-preview-src="" data-preview-group="1"/>');
		}, function(err) {});
	}
	function appendByGalleryMul() {
		plus.gallery.pick(function(path) {
			var html = '<img src="'+e.files[i]+'" data-preview-src="" data-preview-group="1"/>';
			$('.zz-img').append(html);
		}, function(error){}, {} );
	}
	// 添加照片        
	var upload_pic = []; // 照片数组
	var pic_index = 1;
	//点击提交
	$('.tijiao').on('tap', function(){
		upload_pic = [];
		pic_index = 1;
		$('.zz-img img').each(function(){
			var p = $(this).attr('src');
			upload_pic.push({name:"uploadkey"+pic_index,path:p});
			pic_index++;
		})
		if(upload_pic.length >= 1){
			showWating();
			var server = apiRoot + '?m=Home&c=Bbs&a=uploadActivityPic';
			var task = plus.uploader.createUpload(server,{method: "POST"},function(t, status) { 
				if (status == 200) {
					pic = $.parseJSON(t.responseText);
					console.log(pic);
					if(pic.msg){
						errortoast(pic.msg);console.log(JSON.stringify(pic));
						return;
					}
					
					var ordernum = uid+new Date().getTime();//订单号
					$.ajax({
						type:"post",
						url:apiRoot+"?m=home&c=medicinal&a=addNotes",
						data :{
							uid :aid,
							fid :aid,
							did :uid,
							name :name,
							history :histor,
							ztime :ztime,
							newly :newly,
							time :time,
							num :num,
							eatnotice :eatnotice,
							runnotice :runnotice,
							doctorname :doctorname,
							davatar :davatar,
							wtype :wtype,
							ordernum :ordernum,
							status :0,
							camera :pic
						},
						dataType :'json',
						success:function(data){
							plus.nativeUI.closeWaiting(); 
							console.log(JSON.stringify(data));
							if(spage)plus.webview.getWebviewById(spage).close();
							if(pid)plus.webview.getWebviewById(pid).close();
							setTimeout(function(){
								ws.close();
							},1500)
							toast("上传成功");
						},
						error :function(e){
							console.log(JSON.stringify(e));
							errortoast();
						}
					});
				} else {
					toast('上传图片失败！');
					return;
				}
			});
			for(var i in upload_pic){
				var f=upload_pic[i];
				task.addFile(f.path,{key:f.name});
			} 
			task.start();
		}
	})
}, false)